//
//  YZRegisterVC.m
//  VET
//
//  Created by Lam BG on 2018/6/28.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZRegisterVC.h"

#import "YZUser.h"

#import "YZRegisterVM.h"

#import "YZGradientBtn.h"

#import "TBCityIconFont.h"

// Category
#import "SVProgressHUD+Message.h"

@interface YZRegisterVC ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *emptyHeightConst;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *emptyHeightConstSmall;


@property (weak, nonatomic) IBOutlet UITextField *numberTF;
@property (weak, nonatomic) IBOutlet UIView *numberView;

@property (weak, nonatomic) IBOutlet UITextField *verCodeTF;
@property (weak, nonatomic) IBOutlet UIView *verCodeView;
@property (weak, nonatomic) IBOutlet YZGradientBtn *verCodeBtn;

@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIView *passwordView;

@property (weak, nonatomic) IBOutlet YZGradientBtn *registerBtn;
@property (weak, nonatomic) IBOutlet UIButton *weChatBtn;


@property (strong, nonatomic) YZRegisterVM *registerVM;

@end

@implementation YZRegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - private function

- (void)setup {
    [super setup];
    
    // vc title
    self.title = @"欢迎注册";
    
    // 左上角的按钮
//    UIBarButtonItem *popToPreButton = [self barButtonItemWithImage:[UIImage iconWithInfo:TBCityIconInfoMake(@"\ue600", 30, [UIColor blackColor])] highImage:nil target:self action:@selector(popToPre)];
//    self.navigationItem.leftBarButtonItem = popToPreButton;
    
    
    // 适配分辨率
    if (kIsiPhoneSmall || kIsiPhone6) {
        
        self.emptyHeightConst.constant = YZRealValue(20);
        if (kIsiPhoneSmall) {
            
            self.emptyHeightConstSmall.constant = YZRealValue(8);
        }
    }
}

- (void)bindVM {
    
    RAC(self.registerVM, number) = self.numberTF.rac_textSignal;
    RAC(self.registerVM, password) = self.passwordTF.rac_textSignal;
    RAC(self.registerVM, verCode) = self.verCodeTF.rac_textSignal;
    
    @weakify(self)
    // 错误信息提示
    [[RACObserve(self.registerVM, numberWarningInfo) distinctUntilChanged] subscribeNext:^(NSString *msg) {
        
        if (msg.length > 0) {
            
            [SVProgressHUD yz_showMessage:msg];
        }
    }];
    
    
    // 验证码
    RAC(self.verCodeBtn, enabled) = RACObserve(self.registerVM, canVerCode);
    [[self.verCodeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        @strongify(self)
        [self.registerVM.verCodeCmd execute:nil];
    }];
    [[self.registerVM.verCodeCmd executionSignals] subscribeNext:^(RACSignal<id> *signal) {

        [signal subscribeNext:^(NSString *uid) {

            @strongify(self)
            [YZUser sharedInstance].uniqueId = uid;
//            yingzi-6fa9c52d-65d9-46fe-a29d-7a396d55726a---721082
            
#if DEBUG
            if ([uid containsString:@"---"]) {
                
                NSString *verCode = [uid substringFromIndex:[uid rangeOfString:@"---"].location + 3];
                self.verCodeTF.text = verCode;
                self.registerVM.verCode = verCode;
            }
#endif
        } completed:^{
            
            @strongify(self)
            [self.verCodeBtn startCount:60];
        }];
    }];
    
    
    // 注册按钮
    RAC(self.registerBtn, enabled) = self.registerVM.registerSignal;
    [[self.registerBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        @strongify(self)
        [self.registerVM.registerCmd execute:nil];
    }];
    [[self.registerVM.registerCmd executionSignals] subscribeNext:^(RACSignal<id> *signal) {
        
        [signal subscribeNext:^(NSString *uid) {
            
            @strongify(self)
            // 注册成功后，跳转到上传图片和信息
            UIStoryboard *personalStoryboard = [UIStoryboard storyboardWithName:@"YZLaunchSB" bundle:nil];
            UIViewController *vc = [personalStoryboard instantiateViewControllerWithIdentifier:@"YZAvatarVC"];
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }];
    
    
    // 微信注册
    self.weChatBtn.rac_command = self.registerVM.weChatCmd;
    [[self.weChatBtn.rac_command executionSignals] subscribeNext:^(RACSignal<id> * signal) {
        
        [signal subscribeNext:^(NSString *uid) {
            
            @strongify(self)
            // 注册成功后，跳转到上传图片和信息
            UIStoryboard *personalStoryboard = [UIStoryboard storyboardWithName:@"YZLaunchSB" bundle:nil];
            UIViewController *vc = [personalStoryboard instantiateViewControllerWithIdentifier:@"YZSecurityVC"];
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }];
}

#pragma mark - event actions

- (void)popToPre {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - getter 懒加载

- (YZRegisterVM *)registerVM {
    
    if (_registerVM == nil) {
        
        _registerVM = [[YZRegisterVM alloc] init];
    }
    
    return _registerVM;
}

@end
